package cn.xiongxf.service.impl;

import cn.xiongxf.constant.RedisKeyConstant;
import cn.xiongxf.domain.Blacklist;
import cn.xiongxf.exception.GlobalCustomException;
import cn.xiongxf.mapper.BlacklistMapper;
import cn.xiongxf.service.IBlacklistService;
import cn.xiongxf.util.StrUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiongxiaofu
 * @since 2022-08-17
 */
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements IBlacklistService {
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public boolean insert(Blacklist blacklist) {
        // 判断当前黑名单中的phone,ip,userId这三者在Mysql中是否全存在
        String phone = blacklist.getPhone();
        String ip = blacklist.getIp();
        Long userId = blacklist.getUserId();

        Blacklist blacklistPhone = null;
        Blacklist blacklistIp = null;
        Blacklist blacklistUserId = null;
        if (phone != null){
            EntityWrapper<Blacklist> wrapperPhone = new EntityWrapper<>();
            wrapperPhone.eq("phone", phone);
            blacklistPhone = selectOne(wrapperPhone);
        }
        if (ip != null){
            EntityWrapper<Blacklist> wrapperIp = new EntityWrapper<>();
            wrapperIp.eq("ip", ip);
            blacklistIp = selectOne(wrapperIp);
        }
        if (userId != null){
            EntityWrapper<Blacklist> wrapperUserId = new EntityWrapper<>();
            // 表字段user_id，写成userId会报错：
            // ### SQL: SELECT  id AS id,user_id AS userId,phone,ip,send_time AS sendTime,reason  FROM t_blacklist   WHERE  (userId = ?)
            wrapperUserId.eq("user_id", userId);
            blacklistUserId = selectOne(wrapperUserId);
        }

        if (       (phone == null || blacklistPhone != null)
                && (ip == null || blacklistIp != null)
                && (userId == null || blacklistUserId != null)
           ){
            // 三者全存在，就不需要将当前黑名单对象保存到MySql中，抛异常
            throw new GlobalCustomException("此黑名单信息已存在!");
        }
        // 三者不全存在，将当前黑名单对象保存到MySql中
        // 先执行MySQL的写操作
        boolean b = super.insert(blacklist);
        // 再清空Redis中缓存的黑名单
        redisTemplate.delete(RedisKeyConstant.BLACKLIST_KEY);
        return b;
    }

    @Override
    public boolean deleteById(Serializable id) {
        // 先执行MySQL的写操作
        boolean b = super.deleteById(id);
        // 再清空Redis中缓存的黑名单
        redisTemplate.delete(RedisKeyConstant.BLACKLIST_KEY);
        return b;
    }

    @Override
    public boolean updateById(Blacklist entity) {
        // 先执行MySQL的写操作
        boolean b = super.updateById(entity);
        // 再清空Redis中缓存的黑名单
        redisTemplate.delete(RedisKeyConstant.BLACKLIST_KEY);
        return b;
    }

}
